﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.Remoting;
using System.Text;

namespace RemoteImaging.LicensePlate
{
    static class SuspectCarService
    {

        public static bool IsSuspectCar(string plateNumber)
        {
            var baseUrl = string.Format("http://{0}/api/", Properties.Settings.Default.SuspectCarCheckServerIpPort);
            var client = new HttpClient();
            client.BaseAddress = new Uri(baseUrl);
            var q = Uri.EscapeDataString(string.Format("HPHM='{0}'", plateNumber));
            var request = string.Format("stolenvehicle/?q={0}", q);

            var response = client.GetAsync(request).Result;
            if (response.IsSuccessStatusCode)
            {
                var dic = response.Content.ReadAsAsync<Dictionary<string, string>>().Result;
                if (dic != null)
                {
                    return dic.ContainsKey("HPHM") && dic["HPHM"] == plateNumber;
                }

                return false;
            }
            else
            {
                throw new ServerException(string.Format("server failed with status code: {0}", response.StatusCode));
            }


        }


        public static void SendSms(string message)
        {
            var request = new SendBulkSmsRequest();
            request.recvNumbers = Properties.Settings.Default.SuspectCarReceiveNumber;
            request.content = message;
            request.delayMinute = 0;
            request.flag = 1;
            request.idMessage = Guid.NewGuid().ToString().Replace("-", "");
            request.idSystem = "jjcs";
            request.pwdSystem = "jjcs";
            var proxy = new XD_BMS_WBSService();
            proxy.Url = Properties.Settings.Default.SmsPortalUrl;

            var response = proxy.XDBulkSendMessage(request);
            if (response.flag != 0)
            {
                throw new Exception(response.flag == 1 ? response.message : response.info);
            }
        }
    }
}
